package ar.edu.uba.fi.jmit.aspecter.monitor.objectCount;

import java.util.HashMap;
import java.util.Map;

import javassist.expr.Expr;
import ar.edu.uba.fi.jmit.aspecter.core.Constants;
import ar.edu.uba.fi.jmit.aspecter.core.CoreUtils;
import ar.edu.uba.fi.jmit.aspecter.core.DefinitionFactory;
import ar.edu.uba.fi.jmit.aspecter.core.ResourceProperties;
import ar.edu.uba.fi.jmit.aspecter.core.interceptor.NewInstanceAccessInterceptor;

public class ObjectCountNewInstanceInterceptor extends NewInstanceAccessInterceptor {

	private String monitorName;
	
	private String priority = ResourceProperties.getProperty(Constants.EVENT_DEFAULT_PRIORITY);
	
	public ObjectCountNewInstanceInterceptor(String className, String monitorName, String priority) throws ClassNotFoundException {
		this(className, monitorName);
		this.priority = priority;
	}
	
	public ObjectCountNewInstanceInterceptor(String className, String monitorName) throws ClassNotFoundException {
		super(DefinitionFactory.createConstructor(className));
		this.monitorName = monitorName;
	}

	@Override
	protected String aspectStatementAfter() {
		return "";
	}

	@Override
	protected String aspectStatementBefore() {
		return this.createAndSendEvent();
	}

	private String createAndSendEvent() {
		StringBuffer buff = new StringBuffer();
 		Map<String, String> dataMap = new HashMap<String, String>();
 		dataMap.put(ResourceProperties.getProperty(Constants.EVENT_MONITOR_NAME_KEY), "\"" + monitorName + "\"");
 		dataMap.put(ResourceProperties.getProperty(Constants.EVENT_MONITOR_DATE), "new java.util.Date()");
 		CoreUtils.createEventCode(buff, priority, "\"" + getClassName() + "\"", dataMap, true);
 		return buff.toString();
	}

	@Override
	public boolean mustAspect(Expr expression) {
		return true;
	}

	public String getMonitorName() {
		return monitorName;
	}

	public void setMonitorName(String monitorName) {
		this.monitorName = monitorName;
	}

}
